/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gem.controller;

import gem.model.core.Course;
import gem.model.core.Department;
import gem.model.core.ScienceDomain;
import gem.model.dao.DAOFactory;
import java.sql.SQLException;
import java.util.Collection;

/**
 *
 * @author Dusan
 */
public class CoursesReaderWriter
{

    public static boolean addNew(Object parent, String newName)
    {
        boolean isSuccessful = true;

        if (parent instanceof Department)
        {
            Department p = (Department) parent;
            ScienceDomain sd = new ScienceDomain().setTitle(newName);
            p.addScienceDomain(sd);
            DAOFactory fac = DAOFactory.getDAOFactory(DAOFactory.MYSQL);
            try
            {
                fac.getScienceDomainDAO().insertScienceDomain(sd);
            }
            catch (SQLException e)
            {
                e.printStackTrace(); // wrap and throw
            }
        }
        else if (parent instanceof ScienceDomain)
        {
            ScienceDomain sd = (ScienceDomain) parent;
            Course c = new Course().setTitle(newName);
            sd.addCourse(c);
            DAOFactory fac = DAOFactory.getDAOFactory(DAOFactory.MYSQL);
            try
            {
                fac.getCourseDAO().insertCourse(c);
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
        else if (parent instanceof String)
        {
            // root menu
            Department d = new Department().setTitle(newName);
            try
            {
                DAOFactory.getDAOFactory(DAOFactory.MYSQL).getDepartmentDAO().insertDepartment(d);
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
        return isSuccessful;
    }

    public static boolean rename(Object node, String newName)
    {
        boolean isSuccessful = true;

        if (node instanceof Department)
        {
            Department d = (Department) node;
            d.setTitle(newName);
            try
            {
                DAOFactory.getDAOFactory().getDepartmentDAO().updateDepartment(d);
            }
            catch (SQLException e)
            {
                e.printStackTrace();
                isSuccessful = false;
            }
        }
        else if (node instanceof ScienceDomain)
        {
            ScienceDomain sd = (ScienceDomain) node;
            sd.setTitle(newName);
            try
            {
                DAOFactory.getDAOFactory().getScienceDomainDAO().updateScienceDomain(sd);
            }
            catch (SQLException e)
            {
                e.printStackTrace();
                isSuccessful = false;
            }
        }
        else if (node instanceof Course)
        {
            Course c = (Course) node;
            c.setTitle(newName);
            try
            {
                DAOFactory.getDAOFactory().getCourseDAO().updateCourse(c);
            }
            catch (SQLException e)
            {
                e.printStackTrace();
                isSuccessful = false;
            }
        }

        return isSuccessful;
    }

    public static boolean remove(Object node)
    {
        boolean isSuccessful = true;
        if (node instanceof Department)
        {
            try
            {
                DAOFactory.getDAOFactory().getDepartmentDAO().deleteDepartment((Department) node);
            }
            catch (SQLException e)
            {
                e.printStackTrace();
                isSuccessful = false;
            }
        }
        else if (node instanceof ScienceDomain)
        {
            try
            {
                DAOFactory.getDAOFactory().getScienceDomainDAO().deleteScienceDomain((ScienceDomain) node);
            }
            catch (SQLException e)
            {
                e.printStackTrace();
                isSuccessful = false;
            }
        }
        else if (node instanceof Course)
        {
            try
            {
                DAOFactory.getDAOFactory().getCourseDAO().deleteCourse((Course) node);
            }
            catch (SQLException e)
            {
                e.printStackTrace();
                isSuccessful = false;
            }
        }
        return isSuccessful;
    }

    public static Collection<Department> readDepartments() throws SQLException
    {
        Collection<Department> deps = null;

        try
        {
            deps = DAOFactory.getDAOFactory().getDepartmentDAO().listDepartments();
            for (Department d : deps)
            {
                d.setScienceDomains(DAOFactory.getDAOFactory().getDepartmentDAO().listScienceDomains(d));
                for (ScienceDomain sd : d.getScienceDomains())
                {
                    sd.setCourses(DAOFactory.getDAOFactory().getScienceDomainDAO().listCourses(sd));
                }
            }
        } finally
        {
        }

        return deps;
    }
}
